#Name: Yuan Li
#AndrewID: yuanli1

######User-defined functions######

#Task 1:


#Global area:
from Tkinter import *


entry = None
text = None

#####################################333



def isSentence(sentence):
	import re

	# contain at least one word, some checks are delayed to next stage
	if len(sentence) < 1:
		return False
	
	# The first letter of the sentence is capital
	if mapCapital(sentence[0]) == False:
		return False

	# Words are never separated by more than one space
	spaceCnt = 0
	for item in sentence:
		if item == " ":
			spaceCnt += 1
		else:
			spaceCnt = 0
		
		if spaceCnt == 2:
			return False
	
	# the end of the sentence will exactly be an exclamation point, a question mark
	# or a period


	if mapPun(sentence[len(sentence)-1])== False:
		return False

	# passed all the checks, return True
	return True

# exam whether a letter will be in the ranger of A-Z
def mapCapital(letter):
	import string
	for item in string.uppercase:
		if letter == item:
			return True
	
	return False

# exam whether a letter is a valid punctuation
def mapPun(letter):
	if letter == ".":
		return True
	elif letter == "!":
		return True
	elif letter == "?":
		return True
	else:
		return False

def countWords(sentence):
	# exam the validation of the sentence
	if isSentence(sentence) == False:
		return -1

	# counting words
	cnt = 0

	for item in sentence:
		if item == " " or mapPun(item):
			cnt += 1

	return cnt

def differentWords(sentence):
	import re

	# exam the validation of the sentence
	if isSentence(sentence) == False:
		return -1

	# count the numnber of the different words
	
	num = countWords(sentence)

	# we will use the list.count to make life easier
	parttern = []
	tmp = ""
	diNum = 0

	for item in sentence:
		# extract the word, and get rid of the " " or pun 
		if item == " " or mapPun(item):
			result = parttern.count(tmp)
			if result == 0:
				diNum += 1

			# add counted word to the list and clear tmp
			parttern.append(tmp)
			tmp = ""

		else:
			tmp = tmp + item

	return diNum



#Task 2:
def count():
	global entry

	cnt = countWords(entry.get())

	display(cnt)


def different():
	global entry
	
	diff = differentWords(entry.get())
	
	display(diff)



def display(content):
	global text

	# return = -1 means it is not a sentence
	if content == -1:
		content = "It is not a sentence!!"

	text.delete("1.0", END)
	text.insert(END, content)



	

def run():
	global entry, text

	# Setup the root
	root = Tk()
	root.geometry("450x100")
	root.title("lab3")

	# Setup the widgets
	inputLabel = Label(root, text="input")
	inputField = Entry(root, width=50)
	outputLabel = Label(root, text="output")
	countButton = Button(root, text="count", command=count)
	outputField = Text(root, width=40, height=1)
	diffButton = Button(root, text="different", command=different)

	# Update the global variable
	entry = inputField
	text = outputField

	# lay out the grid
	inputLabel.grid(row=0, column=0)
	inputField.grid(row=1, column=0, columnspan=2)
	outputLabel.grid(row=2, column=0)
	countButton.grid(row=2, column=1)
	outputField.grid(row=3, column=0, columnspan=1)
	diffButton.grid(row=3, column=1)
	

	# ?? pack the widgets
	#inputLabel.pack()
	#inputField.pack()
	#outputLabel.pack()
	#countButton.pack()
	#diffButton.pack()


	# driver
	root.mainloop()

######Test Functions##############

def testIsSentence():
    print "Testing isSentence...",
    assert(isSentence("Sentece analysis is fun!"))
    assert(not isSentence("This  is  not  correct."))
    assert(not isSentence("An elephant is a mouse with an operating system"))
    assert(not isSentence("this will fail..."))
    print "all tests passed!"

def testCountWords():
    print "Testing countWords...",
    assert(countWords("Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn.")==6)
    assert(countWords("What about malformed  input?") == -1)
    assert(countWords("How about repeated words words?") == 5)
    print "all tests passed!"

def testDifferentWords():
    print "Testing differentWords...",
    assert(differentWords("No words are different here.") == 5)
    assert(differentWords("Only a CA can track down another CA.") == 7)
    assert(differentWords("klein bottle for rent--inquire within. ") == -1)
    print "all tests passed!"

def testRun():
    "Testing run..."
    run()
    #You should write the strings tested in the previous two test into the
    #Input box and then use your GUI to make sure they return the correct
    #answers

# test bench
#testIsSentence()
#testCountWords()
#testDifferentWords()
testRun()
